<?php
/**
 * Model Traits Permssion
 * User: zng
 * Date: 2019/11/15
 * Time: 15:08
 */
namespace app\admin\traits\service;

use app\admin\model\admin\PermissionCategory;
use app\exceptions\OrdinaryException;
use \Exception;

Trait Application {

    /**
     * 获取控制器组
     * @author zx
     * @date 2019-11-19
     * @return array
     */
    public function group():array
    {
        return array_map('basename', glob(env('APP_PATH').'/admin/controller/*/'));
    }

    /**
     * 自动更新权限组
     * @param array $config 权限组配置
     * @author zx
     * @date 2019-11-19
     * @throws OrdinaryException
     * @return bool
     */
    public function updatePermissionCategory( array $config )
    {
        $permissionCategoryPath = PermissionCategory::field('id,path')->select();
        $insert = [];
        foreach ( $config as $path => $_category_name ) {
            $isInsert = true;
            foreach ( $permissionCategoryPath as $_permissionCategoryPath ) {
                if ( $_permissionCategoryPath->path === $path ) {
                    $isInsert = false;
                    $insert[] = [
                        'id' => $_permissionCategoryPath->id,
                        'name' => $_category_name['name'],
                        'url' => $_category_name['path'],
                        'path'=>$path,
                        'status'=>1,
                        'description' => '负责'.$_category_name['name'].'下所有操作'
                    ];
                }
            }
            if ( $isInsert === false ) {
                continue;
            }
            $insert[] = [
                'name' => $_category_name['name'],
                'url' => $_category_name['path'],
                'path'=>$path,
                'status'=>1,
                'create_time'=>time(),
                'description' => '负责'.$_category_name['name'].'下所有操作'
            ];
        }
        if ( $insert ) {
            return $this->savePermissionCategory($insert);
        }
        return true;
    }

    /**
     * 插入权限表
     * @param array $insert 需要插入的数据包
     * @author zx
     * @date 2019-11-19
     * @throws OrdinaryException
     * @return bool
     */
    private function savePermissionCategory( array $insert )
    {
        $permissionCategory = new PermissionCategory();
        try {
            return !$permissionCategory->saveAll($insert)->isEmpty();
        } catch ( Exception $exception ) {
            throw new OrdinaryException('权限组更新失败');
        }
    }

    /**
     * 获取分组下所有控制器
     * @param string $group 分组
     * @author zx
     * @date 2019-11-19
     * @return array
     */
    public function controller( string $group='', array $exclude_controller )
    {
        $module_path = env('APP_PATH').'admin/controller'.($group?'/'.$group:'');
        if ( !is_dir($module_path) ) return [];
        $module_path .= '/*.php';
        $ary_files = glob($module_path);
        $result = [];
        foreach ( $ary_files as $_file ) {
            if ( !is_file($_file) ) continue;
            $controllers = basename($_file, '.php');
            if ( in_array($controllers, $exclude_controller) ) continue;
            $result[] = $controllers;
        }
        return $result;
    }

    /**
     * 获取控制器下所有方法名称
     * @param string controller 控制器名称
     * @return array
     */
    public function action(string $controller)
    {
        if ( !$controller ) return [];
        $controller_class = controller($controller);
        $functions = get_class_methods($controller_class);
        $result = [];
        $exclude_action = [ 'initialize', '__construct', 'registerMiddleware', '__debugInfo' ];
        foreach ( $functions as $_function ) {
            if ( in_array($_function, $exclude_action) ) continue;
            $result[] = $_function;
        }
        return $result;
    }

}